# $Id$
# Maintainer: AndyRTR <andyrtr@archlinux.org>

pkgname=ghostscript
pkgver=9.21
pkgrel=3
pkgdesc="An interpreter for the PostScript language"
arch=('i686' 'x86_64')
license=('AGPL' 'custom')
depends=('libxt' 'libcups' 'fontconfig' 'zlib' 'libpng' 'libjpeg'
          'jbig2dec' 'libtiff' 'lcms2' 'dbus' 'libpaper' 'ijs')
makedepends=('gtk3' 'gnutls')
optdepends=('texlive-core:      needed for dvipdf'
            'gtk3:              needed for gsx')
url="http://www.ghostscript.com/"
source=(https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs${pkgver/./}/ghostscript-${pkgver}.tar.xz
        ghostscript-sys-zlib.patch
        CVE-2017-8291.patch)
options=('!makeflags')
# https://github.com/ArtifexSoftware/ghostpdl-downloads/releases
sha256sums=('2be1d014888a34187ad4bbec19ab5692cc943bd1cb14886065aeb43a3393d053'
            'c08c7e1354aaa243e753517c61ff86a799a49e0177c7bf6fe0029abc693386f6'
            '9cf9b04c274eba318907807b24d813fdfd5e7e2f88352a4b88dfc728a5b1e6c3')

prepare() {
  cd ghostscript-${pkgver}
  # fix build with system zlib
  patch -Np1 -i ${srcdir}/ghostscript-sys-zlib.patch
  # CVE-2017-8291; https://bugs.ghostscript.com/show_bug.cgi?id=697808
  patch -Np1 -i ${srcdir}/CVE-2017-8291.patch
}

build() {
  cd ghostscript-${pkgver}
  
  # force it to use system-libs
  # keep heavily patched included openjpeg, leads to segfault with system openjpeg
  # https://bugs.archlinux.org/task/38226
  rm -rfv cups/libs freetype ijs jbig2dec jpeg lcms2 libpng tiff zlib

  autoconf --force

  ./configure --prefix=/usr \
	--enable-dynamic \
	--with-ijs \
	--with-jbig2dec \
	--without-omni \
	--with-x \
	--with-drivers=ALL\
	--with-fontpath=/usr/share/fonts/Type1:/usr/share/fonts \
	--enable-fontconfig \
	--enable-freetype \
	--enable-openjpeg \
	--without-luratech \
	--with-system-libtiff \
	--with-libpaper \
	--disable-compile-inits #--help # needed for linking with system-zlib
  make so
  make
}

package() {
  cd ghostscript-${pkgver}
  make DESTDIR="${pkgdir}" \
	cups_serverroot="${pkgdir}"/etc/cups \
	cups_serverbin="${pkgdir}"/usr/lib/cups install install-so

  # drop full libs linked gs, prefer gsc dynamically linked against libgs; FS#50422
  ln -sf /usr/bin/gsc "${pkgdir}"/usr/bin/gs

  # install missing doc files # http://bugs.archlinux.org/task/18023
  install -m 644 "${srcdir}"/ghostscript-${pkgver}/doc/{VectorDevices.htm,gs-vms.hlp,gsdoc.el,pscet_status.txt} "${pkgdir}"/usr/share/ghostscript/$pkgver/doc/
  
  install -D -m644 LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE

  # remove unwanted localized man-pages
  rm -rf "$pkgdir"/usr/share/man/[^man1]*

  # remove filters that are now maintained in cups-filters as upstream home
  rm -rf "$pkgdir"/usr/lib/cups/filter/{gstopxl,gstoraster}
}
